package com.ruijixiang.leetcode.everyday.leetcode2024.leetcode202410;

public class leetcode20241005 {
    /**
     * 完成旅途的最少时间：二分法
     * @param time
     * @param totalTrips
     * @return
     */
    public long minimumTime(int[] time,int totalTrips){
        long l=0L,r=Long.MAX_VALUE;
        while(l+1<r){
            long mid=l+(r-l)/2;
            if(check(time,mid,totalTrips)){// 时间富裕
                r=mid;
            }else{
                l=mid;
            }
        }
        return r;
    }

    private boolean check(int[] time,long mid,int totalTrips){
        long sum=0L;
        for(int t : time){
            sum+=mid/t;
            if(sum>=totalTrips) return true;
        }
        return false;
    }
}
